package cn.xinfei.xdecision.feign;

import feign.Client;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
import org.springframework.cloud.openfeign.ribbon.CachingSpringLoadBalancerFactory;
import org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class FeignClientConfig {
//    @Bean
//    public Client feignClient(CachingSpringLoadBalancerFactory cachingSpringLoadBalancerFactory, SpringClientFactory clientFactory){
////        return new LoadBalancerFeignClient(new FeignDefaultClient(), cachingSpringLoadBalancerFactory, clientFactory);
//        return new LoadBalancerFeignClient(new FeignDefaultClient(), cachingSpringLoadBalancerFactory, clientFactory);
//    }

    @Bean
    @ConditionalOnMissingBean({Client.class})
    public Client feignClient(CachingSpringLoadBalancerFactory cachingFactory, SpringClientFactory clientFactory, OkHttpClient okHttpClient) {
        feign.okhttp.OkHttpClient delegate = new feign.okhttp.OkHttpClient(okHttpClient);
        log.info("初始化LoadBalancerFeignClient...");
        return new LoadBalancerFeignClient(delegate, cachingFactory, clientFactory);
    }
}
